import {Options, Vue} from 'vue-class-component';
import {inject} from "vue";
import DictionaryModel from "@/models/DictionaryModel";

@Options({})
export default class DictMixin extends Vue {
    getSystemDict(dictName: string):  DictionaryModel[] {
        const dict: Map<string, DictionaryModel[]> | undefined = inject('globalDict');
        if (dict) {
            const item: DictionaryModel[] | undefined = dict.get(dictName);
            if (item) {
                return item;
            } else {
                return [];
            }
        } else {
            return [];
        }
    }

    getUserDict<T>(dictName: 'CUSTOMER_LIST'| 'MATERIAL_TYPE'| 'BOM_LIST' ):  T[] {
        const dict: Map<string, T[]> | undefined = inject('userDict');
        if (dict) {
            const item: T[] | undefined = dict.get(dictName);
            if (item) {
                return item;
            } else {
                return [];
            }
        } else {
            return [];
        }
    }


    getDictEntity(dictName: string, value: string): string | object {
        const dict: Map<string, { [key: string]: string | DictionaryModel[] }> | undefined = inject('globalDict');
        if (dict) {
            const item = dict.get(dictName + '_FLAT');
            if (item) {
                return item[value] ?? '';
            }
            return '';
        } else {
            return '';
        }
    }
}